<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$arrRepairShops = $this->shopList;
$rPro = new Model_DbTable_Pr_Prproducts();
$stInfo = new Model_DbTable_Roster_Stafflogindetail();
$rMove = new Model_DbTable_Pr_Prstockmovement();
$dateSunday = Model_DatetimeHelper::getThisWeekSunday("");

?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body style="font-family:Arial, Helvetica, sans-serif">
<h2>Repair Parts Stock Take DIfference ( Result = Actual - System) Date:<?php echo $this->date?></h2>
<?php 
foreach($arrRepairShops as $key => $value){
	if($key < 10){
		$shopCode = "0".$key;
	}else{
		$shopCode = $key;
	}
	
	$adNo = "AD".$shopCode.$dateSunday;
	$moveHis = $rMove->getPartsRecordByJobId($adNo);
?>
<h3><?php echo $value;?></h3>
<?php if(!$moveHis){?>
<form method="post" action="/repairparts/stock-take-adjust">
<input type = "hidden" name = "date" id = "date" value = "<?php echo $this->date?>"></input>
<table width="850" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">Code</th>
    <th scope="col">Des</th>
    <th scope="col">Act</th>
    <th scope="col">Sys</th>
    <th scope="col">Difference</th>
    <th scope="col">RRP</th>
    <th scope="col">Sub Total</th>
  </tr>
<?php 
	$subTotal = 0;
	$staff = 0;
	$qtyTotal = 0;
	foreach($this->prDiff as $difRow) {

	if($difRow['id_shop'] == $key){

		$staff = $difRow['staff_name_stocktake'];
		if($staff){
			$stLine = $stInfo->getDetail($staff);
		}else{
			$stLine['ni'] = "No Record";
		}
	?>  
  <tr>
    <td><input name ="code_product[]" value="<?php echo $difRow['code_product']?>" type="hidden" /><?php echo $difRow['code_product']?></td>
    <td><?php echo $rPro->getProductDes($difRow['code_product']); ?></td>
    <td align="center"><?php echo $difRow['qty_onhand_cf']; ?></td>
    <td align="center"><?php echo $difRow['qty_onhand']; ?></td>
    <td><input name ="qty_diff[]" value="<?php echo ($difRow['qty_onhand_cf'] - $difRow['qty_onhand']);?>" type="text" style="width:50px;" /> </td>
    <td><?php $rPline = $rPro->getProductByCode($difRow['code_product']); echo $rPline['price_a']; 
	$subTotal += $rPline['price_a'] * ($difRow['qty_onhand_cf'] - $difRow['qty_onhand']);
	$qtyTotal += $difRow['qty_onhand_cf'];	  ?></td>
      <td><?php $rPline = $rPro->getProductByCode($difRow['code_product']); 
	 echo $rPline['price_a'] * ($difRow['qty_onhand_cf'] - $difRow['qty_onhand']);
	  ?></td>
  </tr>
<?php } }?>  
  <tr>
  	<?php if($subTotal != 0){?>
  	<td align="right">
      By: <?php echo $stLine['ni']?>   </td>
  	<td colspan="5" align="right"><input type="hidden" name="shop_code" value="<?php echo $key;?>" />  	  <input type="submit" name="button" id="button" value="Create Adjustment" <?php if($moveHis){?>disabled<?php }?>/></td>
  	<td>$<?php echo $subTotal;?><input type ="hidden" name="total" value ="<?php echo $subTotal;?>"></input></td>
  	<?php }else{?>
  	<td align="right">
      <?php echo 'No Difference'?>   </td>
  	<td colspan="5" align="right"><input type="hidden" name="shop_code" value="<?php echo $key;?>" />  	  <input type="submit" name="button" id="button" value="Create Adjustment" <?php if($qtyTotal == 0){?>disabled<?php }?>/></td>
  	<td>$<?php echo 0;?></td>
  	<?php }?>
  </tr>
</table>
<?php }else{ 
		$total = 0;?>
	<h3>Adjust History</h3>
	<table width="650" border="1" cellspacing="0" cellpadding="5">
  <tr>
    <th scope="col">Code</th>
    <th scope="col">Description</th>
    <th scope="col">Qty</th>
    <th scope="col">RRP</th>
  </tr>
  <?php foreach($moveHis as $k => $v){
  	
	 if($v['code_move']!=9){  
	  ?>
 
  <tr>
    <td><?php 
    $pLine = $rPro->getProduct($v["id_product"]);
    echo $pLine["code_product"];
    ?></td>
    <td style="font-size:12px;"><?php echo $pLine["title_product"]?></td>
    <td><?php 
	echo ($v['code_move']==6)?"- ":"";
	echo $v["qty_product"]?></td>
	<td align="right"><?php 
			if($v['code_move'] ==6){
				$cost = (0-$v['qty_product'])*$pLine['price_a'];
	 		}else{
	 			$cost = $v['qty_product']*$pLine['price_a'];
	 		}
			$total += $cost;
			echo $cost;?></td>
  </tr>
  <?php
	 }
  
   }?>
   <tr>
   	<td colspan = "4" align="right"><?php echo "Total:$ ".$total;?></td>
   </tr>
</table>
<?php }?>
</form>
<?php 
}?>
</body>
</html>
